//
//  LQYItemsDetailViewController.m
//  FAKER
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "LQYItemsDetailViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "NSString+ChangeHTMLString.h"
@interface LQYItemsDetailViewController ()<backToPresentViewControllerDelegate, UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)NSURL *url;

@property(nonatomic, retain)YHLoadingView *loadingView;
@end

@implementation LQYItemsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _loadingView = [[YHLoadingView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40)];
    if (NSUSERDEFAULTS_NIGHT) {
        _loadingView.backgroundColor = [UIColor blackColor];
    }
    else{
        _loadingView.backgroundColor = [UIColor whiteColor];
    }
    
    [self.view addSubview:_loadingView];
}

-(void)loadView
{
    [super loadView];
    

    LQYBottomToolView *bottomToolView = [[LQYBottomToolView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40 * FITHEIGHT, SCREEN_WIDTH, 40 * FITHEIGHT)];
    bottomToolView.backDelegate = self;
    [self.view addSubview:bottomToolView];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    label.backgroundColor = [UIColor colorWithRed:0.33 green:0.47 blue:0.58 alpha:1];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, SCREEN_WIDTH - 10, 70)];
    titleLabel.text = _carryTitleString;
    titleLabel.font = [UIFont systemFontOfSize:25];
    titleLabel.textColor = [UIColor whiteColor];

    
    UILabel *autherLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, titleLabel.frame.origin.y + titleLabel.frame.size.height + 5, SCREEN_WIDTH - 10, 30)];
    autherLabel.text = _carryAutherString;
    autherLabel.textColor = [UIColor whiteColor];
    

    
    
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 41 * FITHEIGHT)];
    _webView.delegate = self;
    //根據字符串是否為空 判斷是否為廣告接口
    if (_carryAdUrl == nil){
        //根據value個數以及字符串樣式判斷接口
        if (_count == 10 && [_carryCellStyle isEqualToString:@"all_pic"]){
            [self getData];
        }
        else{
            //根據添加的唯一標識判斷是何種接口
            if (_tag == 1){
                _url = [NSURL URLWithString:_carryWeekendUrl];
            }
            else{
                if (_count == 13){
                    _url = [NSURL URLWithString:_articleModal.weburl];
                }
                else{
                    [self getData];
                }
            }
        }
    }
    else{
        _url = [NSURL URLWithString:_carryAdUrl];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    [_webView loadRequest:request];
    if (_count == 10) {
        [_webView.scrollView.subviews lastObject].frame = CGRectMake(0, 200, SCREEN_WIDTH, SCREEN_HEIGHT - 241);
        [_webView.scrollView addSubview:label];
        [_webView.scrollView addSubview:titleLabel];
        [_webView.scrollView addSubview:autherLabel];
    }
    [self.view addSubview:_webView];

   
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_loadingView removeFromSuperview];
    if (NSUSERDEFAULTS_NIGHT) {
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [_loadingView createRotationAnimation];
}
#pragma mark    獲取數據
-(void)getData
{
    [InternetDataRequest getDataByString:_articleModal.full_url BodyDic:nil WithDataBlock:^(id data) {
        NSString *htmlString = [NSString changeHTMLStringWithDataDictionary:data];
        [_webView loadHTMLString:htmlString baseURL:nil];
    }];
}
//執行協議方法 進行頁面跳轉
-(void)backToPresentViewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString isEqualToString:@"http://www.myzaker.com/m/"]) {
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
